package com.example.widgethost.base;

import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.example.widgethost.base.cache.CacheInterface;
import com.example.widgethost.base.cache.CacheWidgetBean;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CacheTool implements CacheInterface {
    private static final String TAG = "CacheTool";
    private final SharedPreferences preferences;
    private Gson gson;
    private static final String PREF_KEY = "widgets_added";

    /**
     * constructor.
     *
     * @param context context
     */
    public CacheTool(Context context) {
        preferences = context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
        gson = new GsonBuilder().create();
    }

    /**
     * save added widgets info.
     *
     * @param newAppWidgetId widget id
     * @param componentName  component name
     */
    @Override
    public void cacheWidget(int newAppWidgetId, ComponentName componentName) {
        Log.e(TAG, "cacheWidget newAppWidgetId " + newAppWidgetId + "//componentName " + componentName.flattenToString());
        String flattenComponentName = componentName.flattenToString();
        CacheWidgetBean cacheWidgetBean = getCacheWidgetBean(flattenComponentName);
        if (cacheWidgetBean != null) {
            List<Integer> widgetIds = cacheWidgetBean.widgetIds;
            if (!widgetIds.contains(newAppWidgetId)) {
                widgetIds.add(newAppWidgetId);
            }
            cacheWidgetBean.widgetIds = widgetIds;
        } else {
            List<Integer> widgetIds = new ArrayList<>();
            widgetIds.add(newAppWidgetId);
            cacheWidgetBean = new CacheWidgetBean();
            cacheWidgetBean.widgetIds = widgetIds;
            cacheWidgetBean.componentName = componentName;
        }

        String json = gson.toJson(cacheWidgetBean);
        Log.e(TAG, "cacheWidget cached json  " + json);
        preferences.edit().putString(flattenComponentName, json).apply();
    }

    /**
     * get added widgets info.
     *
     * @return widget info list
     */
    @Override
    public List<CacheWidgetBean> getCachedWidgetBeans() {
        return getAllCacheWidgets();
    }

    private List<CacheWidgetBean> getAllCacheWidgets() {
        List<CacheWidgetBean> lists = new ArrayList<>();
        Map<String, String> map = (Map<String, String>) preferences.getAll();
        if (map != null && map.size() > 0) {
            for (String value : map.values()) {
                Log.d(TAG, value);
                CacheWidgetBean cacheWidgetBean = gson.fromJson(value, CacheWidgetBean.class);
                lists.add(cacheWidgetBean);
            }
        }
        return lists;
    }

    private CacheWidgetBean getCacheWidgetBean(String flattenComponentName) {
        String strCacheWidgetBean = preferences.getString(flattenComponentName, null);
        CacheWidgetBean cacheWidgetBean = null;
        if (!TextUtils.isEmpty(strCacheWidgetBean)) {
            cacheWidgetBean = gson.fromJson(strCacheWidgetBean, CacheWidgetBean.class);
        }
        return cacheWidgetBean;
    }

}
